package com.iweb.address.dao;

import com.iweb.address.model.Address;
import com.iweb.util.JdbcUtil;
import com.iweb.util.PageModel;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class AddressDaoImpl implements AddressDao {
    @Override
    public List<Address> selectByPage(PageModel pageModel, Integer userid) {
        List<Address> addresses = new ArrayList<>();
        String likeValue = pageModel.getLikeValue();
        StringBuffer sql = new StringBuffer("select addressid,userid,phone,address,userfullname from tb_address " +
                "where userid = ? and (phone like ? or address like ?)");
        int recordCount = JdbcUtil.selectBySql(sql.toString(), userid,
                "%" + likeValue + "%", "%" + likeValue + "%").size();
        pageModel.setRecordCount(recordCount);
        sql.append(" order by addressid desc limit ?,?");
        int startNo = pageModel.getPageSize() * (pageModel.getPageNo() - 1);
        List<Map<String, Object>> list = JdbcUtil.selectBySql(sql.toString(), userid,
                "%" + likeValue + "%", "%" + likeValue + "%", startNo, pageModel.getPageSize());
        for (int i = 0; i < list.size(); i++) {
            Map<String, Object> map = list.get(i);
            Integer addressid = (Integer) map.get("addressid");
            String phone = (String) map.get("phone");
            String address = (String) map.get("address");
            String userfullname = (String) map.get("userfullname");
            addresses.add(new Address(addressid, userid, userfullname, phone, address));
        }
        return addresses;
    }

    @Override
    public Address queryByUserId(Integer userid) {
        String sql = "select addressid,userid,phone,address,userfullname from tb_address where userid = ?";
        List<Map<String, Object>> list = JdbcUtil.selectBySql(sql, userid);
        Map<String, Object> map = list.get(0);
        Integer addressid = (Integer) map.get("addressid");
        String phone = (String) map.get("phone");
        String address = (String) map.get("address");
        String userfullname = (String) map.get("userfullname");
        return new Address(addressid, userid, userfullname, phone, address);
    }

    @Override
    public int saveOne(Address address) {
        String sql = "insert into tb_address values(null,?,?,?,?) ";
        return JdbcUtil.editBySql(sql, address.getUserid(), address.getUserfullname(),address.getPhone(), address.getAddress());
    }

    @Override
    public int deleteById(Integer addressid) {
        String sql = "delete from tb_address where addressid = ?";
        return JdbcUtil.editBySql(sql, addressid);
    }

    @Override
    public int updateById(Address address) {
        String sql = "update tb_address set userfullname = ?, phone = ?,address = ? where addressid = ?";
        return JdbcUtil.editBySql(sql,address.getUserfullname(), address.getPhone(), address.getAddress(), address.getAddressid());
    }

    @Override
    public Address queryByAddressId(Integer addressid) {
        String sql = "select addressid,userid,phone,address,userfullname from tb_address where addressid = ?";
        List<Map<String, Object>> list = JdbcUtil.selectBySql(sql,addressid);
        Map<String, Object> map = list.get(0);
        Integer userid = (Integer) map.get("userid");
        String phone = (String) map.get("phone");
        String address = (String) map.get("address");
        String userfullname = (String) map.get("userfullname");
        return new Address(addressid, userid, userfullname, phone, address);
    }
}
